package com.ybl.library;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 日期：16/6/22 15:47
 * <p/>
 * 描述：StepView
 */
public class ProInvestCycleStepView extends LinearLayout implements ProInvestCycleStepsViewIndicator.OnDrawIndicatorListener {
    private RelativeLayout mTextContainerTop, mTextContainerBottom, mTextContainerBottom2;
    private ProInvestCycleStepsViewIndicator mStepsViewIndicator;

    private List<Float> complectedXPosition;
    private List<Integer> mCircleColors;
    private List<String> mTopTexts;
    private List<String> mBottomTexts;
    private List<String> mBottom2Texts;

    private TextView textView;

    public ProInvestCycleStepView(Context context) {
        this(context, null);
    }

    public ProInvestCycleStepView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProInvestCycleStepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.widget_pro_invest_cycle_stepsview, this);
        mStepsViewIndicator = (ProInvestCycleStepsViewIndicator) rootView.findViewById(R.id.steps_indicator);
        mStepsViewIndicator.setOnDrawListener(this);
        mTextContainerTop = (RelativeLayout) rootView.findViewById(R.id.rl_text_container_top);
        mTextContainerBottom = (RelativeLayout) rootView.findViewById(R.id.rl_text_container_bottom);
        mTextContainerBottom2 = (RelativeLayout) rootView.findViewById(R.id.rl_text_container_bottom2);
    }

    /**
     * 设置显示的文字
     *
     * @param circleColors
     * @return
     */
    public ProInvestCycleStepView setStepColors(List<Integer> circleColors) {
        if (circleColors == null) {
            circleColors = new ArrayList<>();
            circleColors.add(Utils.getColor(getContext(), R.color.color_pro_invest_cycle_start));
            circleColors.add(Utils.getColor(getContext(), R.color.color_pro_invest_cycle_end));
        }

        mCircleColors = circleColors;
        mStepsViewIndicator.setStepNum(circleColors);
        return this;
    }

    public ProInvestCycleStepView setStepViewTopTexts(List<String> topTexts) {
        mTopTexts = topTexts;
        return this;
    }

    public ProInvestCycleStepView setStepViewBottomTexts(List<String> bottomTexts) {
        mBottomTexts = bottomTexts;
        return this;
    }

    public ProInvestCycleStepView setStepViewBottom2Texts(List<String> bottom2Texts) {
        mBottom2Texts = bottom2Texts;
        return this;
    }


    @Override
    public void ondrawIndicator() {
        if (mTextContainerTop != null || mTextContainerBottom != null || mTextContainerBottom2 != null) {
            complectedXPosition = mStepsViewIndicator.getCircleCenterPointPositionList();
            if (mCircleColors != null && complectedXPosition != null && complectedXPosition.size() > 0) {
                if (mTextContainerTop != null && mTopTexts != null) {
                    mTextContainerTop.removeAllViews();
                    addTexts(mTextContainerTop, mTopTexts, 0);
                }

                if (mTextContainerBottom != null && mBottomTexts != null) {
                    mTextContainerBottom.removeAllViews();
                    addTexts(mTextContainerBottom, mBottomTexts, 1);
                }

                if (mTextContainerBottom2 != null && mBottom2Texts != null) {
                    mTextContainerBottom2.removeAllViews();
                    addTexts(mTextContainerBottom2, mBottom2Texts, 2);
                }

            }
        }
    }


    private void addTexts(RelativeLayout rlContainer, List<String> texts, int location) {
        for (int i = 0; i < mCircleColors.size(); i++) {
            textView = new TextView(getContext());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                    location == 1 ? 13 : 12);
            textView.setText(texts.get(i));
            textView.setTextColor(
                    location == 0 ?
                            (i == 0 ?
                                    mCircleColors.get(0) : Utils.getColor(getContext(), R.color.color_4F)
                            )
                            :
                            location == 2 ?
                                    Utils.getColor(getContext(), R.color.color_95) : Utils.getColor(getContext(), R.color.color_4F)
            );
            int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            textView.measure(spec, spec);
            // getMeasuredWidth
            int measuredWidth = textView.getMeasuredWidth();
            textView.setX(complectedXPosition.get(i) - measuredWidth / 2);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            rlContainer.addView(textView);
        }
    }


}
